package com.rc.evcharger.biz.log;

import com.google.gson.Gson;
import com.rc.evcharger.service.log.ChargedProcessLogService;
import com.rc.evcharger.vo.log.ChargedProcessLog;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 充电日志
 * Create by Yang on 2019/11/21
 */
@Component("chargedProcessLogListener")
public class ChargedProcessLogListener implements ChannelAwareMessageListener {
    private static Logger logger = LoggerFactory.getLogger(ChargedProcessLogListener.class);

    @Autowired
    private ChargedProcessLogService chargedProcessLogService;

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        String msg = new String(message.getBody(), "UTF-8");
        logger.info("chargedProcessLogListener 收到了:{}", msg);

        try {
//           alibaba的解析不完整
//          ChargedProcessLog log = JSON.parseObject(msg, ChargedProcessLog.class);

            ChargedProcessLog log = new Gson().fromJson(msg, ChargedProcessLog.class);

            //新增的MONGODB LOG
            chargedProcessLogService.addChargedProcessLog(log);
        } catch (Exception e) {
            logger.error("chargedProcessLogListener 收到了异常的JSON", e);
        }
    }
}
